package muilty_thread.pic_java_design_pattern.balking;

import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/10/12 23:32
 */


public class Data {
    private final String fileName;
    private String content;
    private boolean changed;

    public Data(String fileName, String content, boolean changed) {
        this.fileName = fileName;
        this.content = content;
        this.changed = changed;
    }

    public synchronized void change(String newContent) {
        System.out.println("changed");
        content = newContent;
        changed = true;
    }
    public synchronized void save() throws IOException {
        if (!changed) {
            return;
        }
        doSave();
        changed = false;

    }

    private void doSave() throws IOException {
        System.out.println(Thread.currentThread().getName() + "do Save Content:" + content);
        FileWriter fileWriter = new FileWriter(fileName);
        fileWriter.write(content);
        fileWriter.close();

    }

    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(1);
    }
}
